package org.zp.handler;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.UpdateTypeName;
import org.zp.dto.UpdateTypeState;
import org.zp.pojo.AdminInfo;
import org.zp.pojo.TypeInfo;
import org.zp.service.AdminLogInfoService;
import org.zp.service.TypeInfoService;
import org.zp.util.Ajax;

@RestController
public class TypeHandler {
@Resource
private TypeInfoService t;
@Resource
private AdminLogInfoService log;

private String url = "类型列表";
/**
 * 获取类型的集合
 * @param req
 * @param resp
 */
@GetMapping("getTypes.act")
private void getTypes(HttpServletRequest req, HttpServletResponse resp) {
	List<TypeInfo> list = t.showTypes();
	
	Ajax.success(resp, "list", list);
}
/**
 * 类型的禁用启用
 * @param dto
 * @param req
 * @param resp
 */
@PostMapping("updateTypeState.act")
private void updateTypeState(UpdateTypeState dto,HttpServletRequest req, HttpServletResponse resp) {
	Integer jid = Integer.parseInt(dto.getJid());
	Integer pid = Integer.parseInt(dto.getPid());
	Integer state = Integer.parseInt(dto.getState());
	System.out.println("jid:"+jid);
	System.out.println("pid:"+pid);
	System.out.println("state:"+state);
	Integer flag = t.updateTypeState(jid,pid,state);
	if(flag>0) {
		System.out.println("保存成功");
		AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid1 = admin1.getAdminId();
		Integer cid = 3;//此为修改的操作id
		Integer logs = log.addlog(aid1,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}
}
/**
 * 类型的名字更改
 * @param dto
 * @param req
 * @param resp
 */
@PostMapping("updateTypeName.act")
private void updateTypeName(UpdateTypeName dto,HttpServletRequest req, HttpServletResponse resp) {
	Integer jid = Integer.parseInt(dto.getJid());
	String name = dto.getMenuName();
	System.out.println(jid);
	System.out.println(name);
	Integer flag = t.updateTypeName(jid,name);
	if(flag>0) {
		System.out.println("保存成功");
		AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid1 = admin1.getAdminId();
		Integer cid = 3;//此为修改的操作id
		Integer logs = log.addlog(aid1,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}
}
/**
 * 移动至根目录
 * @param jid
 * @param req
 * @param resp
 */
@PostMapping("updateTypeJid.act")
private void updateTypeJid(String jid,HttpServletRequest req, HttpServletResponse resp) {
	Integer jidd = Integer.parseInt(jid);
	System.out.println("主键id"+jidd);
	Integer flag = t.updateTypeJid(jidd);
	if(flag>0) {
		System.out.println("保存成功");
		AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid1 = admin1.getAdminId();
		Integer cid = 3;//此为修改的操作id
		Integer logs = log.addlog(aid1,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}
}
/**
 * 自由移动类型
 * @param moveId
 * @param moveJuriPid
 * @param req
 * @param resp
 */
@PostMapping("moveType.act")
private void moveType(String moveId,String moveJuriPid,
		HttpServletRequest req, HttpServletResponse resp) {
	Integer moveIdd =0;
	Integer moveJuriPidd = 0;
	try {
		 moveIdd = Integer.parseInt(moveId);
		 moveJuriPidd = Integer.parseInt(moveJuriPid);
	} catch (Exception e) {
		Ajax.error(resp, "菜单相关id没获取到");

	}
	
	Integer flag = t.moveType(moveIdd,moveJuriPidd);
	if(flag>0) {
		System.out.println("保存成功");
		AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid1 = admin1.getAdminId();
		Integer cid = 3;//此为修改的操作id
		Integer logs = log.addlog(aid1,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}
}
/**
 * 类型的添加
 * @param typeName
 * @param typeMoney
 * @param req
 * @param resp
 */
@PostMapping("addType.act")
private void addType(
		String typeName,
		String typeMoney,
		HttpServletRequest req, HttpServletResponse resp) {
	Integer tMoney = 0;
	if (typeMoney.equals("")||typeMoney==null) {
		tMoney = 0;
		Integer flag = t.addTypeWithMoney(typeName,tMoney);
		if(flag>0) {
			System.out.println("保存成功");
			AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
			Long aid1 = admin1.getAdminId();
			Integer cid = 1;//此为添加的操作id
			Integer logs = log.addlog(aid1,cid,url);
			if (logs>0) {
				System.out.println("加入日志成功");
			}else {
				System.out.println("加入日志失败");
				return;
			}
			Ajax.success(resp);
			return;
			}else {
			Ajax.error(resp,"保存失败");
			return;
			}
	}else if(!typeMoney.equals("")){
		try {
			tMoney = Integer.parseInt(typeMoney);
		} catch (Exception e) {
			Ajax.error(resp,"输入错误");
			return;
		}
		Integer flag = t.addTypeWithMoney(typeName,tMoney);
		if(flag>0) {
			System.out.println("保存成功");
			AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
			Long aid1 = admin1.getAdminId();
			Integer cid = 1;//此为添加的操作id
			Integer logs = log.addlog(aid1,cid,url);
			if (logs>0) {
				System.out.println("加入日志成功");
			}else {
				System.out.println("加入日志失败");
				return;
			}
			Ajax.success(resp);
			return;
			}else {
			Ajax.error(resp,"保存失败");
			return;
			}
	}

}
/**
 * 删除类型
 * @param jid
 * @param req
 * @param resp
 */
@PostMapping("deleteType.act")
private void deleteType(String jid,HttpServletRequest req, HttpServletResponse resp) {
	Integer jidd = Integer.parseInt(jid);
	Integer flag = t.deleteType(jidd);
	if(flag>0) {
		System.out.println("删除成功");
		Ajax.success(resp);
		return;
		}else {
		Ajax.error(resp,"删除失败");
		return;
		}
}
@GetMapping("comboBoxType.act")
private void comboBoxType(HttpServletRequest req, HttpServletResponse resp) {
	List<TypeInfo> list = t.comboBoxType();
	Ajax.success(resp, "list", list);
}
}
